from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import logging
import json
from MusicPlayer import KuGou
from django.template import loader
from .models import Climate
from django.shortcuts import get_object_or_404, reverse
from django.http import HttpResponseRedirect
# Create your views here.
logger = logging.getLogger(__name__)

def firstpage(request):
    return render(request, "firstpage.html")

def get_login_data(data):
    name=data.get("name")
    password=data.get("password")
    #登录
    Player.input_validationNum(data.get('password'))

def ask_for_validcode(data):
    name=data.get("name")
    global Player
    Player=KuGou.KuGouMusic(name)
    Player.Login()
    #获取验证码

@csrf_exempt
def login(request):
    if request.method=="POST":
        data= request.POST #json.loads(request.body.decode('utf-8'))
        for k,v in data.items():
            logger.error("key:"+k)
            logger.error("value:"+v)
        if data.get("mode")=="get_login_data":
            #get_login_data(data)
            #return render(request,"test.html")
            return HttpResponseRedirect('../index/')
            '''template=loader.get_template('index.html')
            climate=Climate()
            weather_background={"晴":"/media/img/weather/sunny.png","小雨":"/media/img/weather/storm.png","多云":"/media/img/weather/cloudy.png","":"/media/img/close.png",}
            weather_background=weather_background.get(climate.weather,"/media/img/weather/sunny.png")
            context={'climate':climate,'weather_background':weather_background}
            return HttpResponse(template.render(context,request))'''
        else:
            ask_for_validcode(data)
    return render(request, "login.html")

def index(request):
    template=loader.get_template('index.html')
    climate=Climate()
    weather_background={"晴":"/media/img/weather/sunny.png","小雨":"/media/img/weather/storm.png","多云":"/media/img/weather/cloudy.png","":"/media/img/close.png",}
    weather_background=weather_background.get(climate.weather,"/media/img/weather/sunny.png")
    context={'climate':climate,'weather_background':weather_background}
    return HttpResponse(template.render(context,request))

def hello_Page(request):
    return redirect('/firstpage/')